package cn.zorbus.laian.infrastructure.common;

import cn.zorbus.laian.infrastructure.common.view.ISimpleView;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

/**
 * create in 2023/12/2 21:48
 *
 * @author shishaodong
 * @version 0.0.1
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    private static final String MESSAGE_SUCCESS = "OK";

    private static final String MESSAGE_FAILURE = "failure";

    private static final String CODE = "000000";

    @JsonView({ISimpleView.class})
    private String message;

    @JsonView({ISimpleView.class})
    private String code;

    @JsonView({ISimpleView.class})
    private T data;

    @Contract(" -> new")
    public static @NotNull Result<Void> success() {
        return Result.<Void>builder()
                .code(CODE)
                .message(MESSAGE_SUCCESS)
                .build();
    }

    public static @NotNull <T> Result<T> success(T data) {
        return Result.<T>builder()
                .code(CODE)
                .data(data)
                .message(MESSAGE_SUCCESS)
                .build();
    }

    @Contract(" -> new")
    public static @NotNull <T> Result<T> failure() {
        return Result.<T>builder().build();
    }
    @Contract(" -> new")
    public static @NotNull <T> Result<T> failure(String message) {
        return Result.<T>builder().message(message).build();
    }

}
